package com.jubaozan.framework.sentinel.stat.traffic.config;

import com.jubaozan.framework.sentinel.stat.traffic.TrafficInterceptor;
import java.util.Optional;
import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author jiangjinwu
 * @version 1.0
 * @since 2025/7/28 17:38
 */

@Slf4j
public class TrafficWebMvcConfigurer implements WebMvcConfigurer {
    @Autowired
    private Optional<TrafficInterceptor> trafficInterceptorOptional;


    @Autowired
    TrafficStatConfig trafficStatConfig;
    public void addInterceptors(InterceptorRegistry registry) {
        if (this.trafficInterceptorOptional.isPresent()) {
            registry.addInterceptor((HandlerInterceptor)this.trafficInterceptorOptional.get()).order(trafficStatConfig.getOrder()).addPathPatterns(trafficStatConfig.getUrlPatterns());
            log.info("[Sentinel Starter] register SentinelWebInterceptor with urlPatterns: {}.", trafficStatConfig.getUrlPatterns());
        }
    }
}
